import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';
import 'package:kq_flutter_core_widget/utils/text_filed_utils.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import 'package:kq_flutter_widgets/theme/kq_theme_manager.dart';

import '../divider/kq_divider.dart';
import '../textFiled/kq_textfiled.dart';

/// 通用输入对话框
///
/// @author 周卓
///
class KqInputDialog extends AlertDialog {
  /// 标题
  final String? titleString;

  /// 默认值
  final String? defValue;

  /// 提示文字，默认"请输入"
  final String? placeHolder;

  /// 当行模式点击键盘换行键是否自动提交
  final bool autoSubmitWhenReturn;

  /// 是否多行，默认false
  final bool multiline;

  /// 最大行数，不传不限制
  final int? maxLines;

  /// 左边按钮文字，默认"取消"
  final String? leftBtnText;

  /// 中间按钮文字，默认null,不传则不显示
  final String? middleBtnText;

  /// 左边按钮文字，默认"确定"
  final String? rightBtnText;

  /// 输入类型，默认[TextInputType.text]
  final TextInputType? inputType;

  /// 输入内容限制
  final List<TextInputFormatter>? inputFormatters;

  /// 输入框文字对齐方式
  final TextAlign? inputTextAlign;

  /// 是否显示清空按钮
  final bool enableClearButton;

  /// 输入回调
  final Function(String value)? onValueChanged;

  /// 左边按钮回调
  final Function(String value)? onLeftBtnTap;

  /// 中间按钮回调
  final Function(String value)? onMiddleBtnTap;

  /// 右边按钮回调
  final Function(String value)? onRightBtnTap;

  /// 输入框内容控制器
  final TextEditingController editController;

  /// 最大字符数，默认不限制
  final int? maxCharCount;

  KqInputDialog({Key? key,
    this.defValue,
    this.titleString,
    this.multiline = false,
    this.autoSubmitWhenReturn = false,
    this.maxLines,
    this.inputFormatters,
    this.placeHolder,
    this.inputTextAlign,
    this.inputType,
    this.leftBtnText,
    this.middleBtnText,
    this.rightBtnText,
    this.enableClearButton = false,
    this.onValueChanged,
    this.onLeftBtnTap,
    required this.editController,
    this.onMiddleBtnTap,
    this.onRightBtnTap,
  this.maxCharCount})
      : super(key: key) {
    if (defValue != null) {
      TextFiledUtil.setValue(editController, defValue!);
    }
  }

  @override
  Widget build(BuildContext context) {

    bool isLangScape = context.orientation == Orientation.landscape;

    final List<Widget> children = <Widget>[];

    /// 上边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    if (titleString != null) {
      children.add(Container(
        padding: EdgeInsets.symmetric(horizontal: 16.r),
        alignment: Alignment.center,
        child: Text(
          titleString!,
          style: TextStyle(
              fontSize: 16.sp,
              color:
              KqThemeManager.instance
                  .getConfig()
                  .dialogConfig
                  .titleColor ??
                  KqThemeColors.text26,
              fontWeight: FontWeight.w600),
        ),
      ));
      children.add(SizedBox(
        width: 1.r,
        height: 24.r,
      ));
    }

    /// 输入框
    children.add(Container(
      margin: EdgeInsets.symmetric(horizontal: 16.r),
      padding: EdgeInsets.symmetric(horizontal: 10.r, vertical: 12.r),
      decoration: BoxDecoration(
          border: Border.all(
              width: 1,
              color:
              KqThemeManager.instance
                  .getConfig()
                  .commonConfig
                  .lineColor ??
                  KqThemeColors.lineColor),
          color: KqThemeColors.bgTransparent),
      child: KqTextFiled(
        multiline: multiline,
        useParentHeight: false,
        placeHolder: placeHolder,
        autofocus: true,
        controller: editController,
        maxLines: maxLines,
        maxCharCount: maxCharCount,
        inputFormatters: inputFormatters,
        enableClearButton: enableClearButton,
        onChanged: (value) {
          // editController.text = value;
          if (onValueChanged != null) {
            onValueChanged!(value);
          }
        },
        onSubmitted: (value) {
          if (onRightBtnTap != null && autoSubmitWhenReturn) {
            onRightBtnTap!(value);
          }
        },
        defValue: defValue,
        inputType: inputType,
        textInputAction: multiline ? TextInputAction.newline : TextInputAction.done,
        textAlign: inputTextAlign ?? TextAlign.start,
      ),
    ));

    /// 下边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    /// 按钮
    children.add(const KqDivider());

    children.add(Row(
      children: [
        Expanded(
            child: KqInkWell(
              onTap: () {
                if (onLeftBtnTap != null) {
                  onLeftBtnTap!(editController.text);
                }
              },
              child: Container(
                alignment: Alignment.center,
                width: double.infinity,
                height: 48.r,
                child: Text(
                  leftBtnText ?? KqIntl.currentResource.cancel,
                  style: TextStyle(
                      fontSize: 16.sp,
                      color: KqThemeManager.instance
                          .getConfig()
                          .dialogConfig
                          .cancelBtnColor ??
                          KqThemeColors.text8C),
                ),
              ),
            )),
        KqVerticalDivider(
          height: 48.r,
        ),
        Visibility(visible: middleBtnText != null, child: Expanded(
          child: KqInkWell(
            onTap: () {
              if (onMiddleBtnTap != null) {
                onMiddleBtnTap!(editController.text);
              }
            },
            child: Container(
              alignment: Alignment.center,
              width: double.infinity,
              height: 48.r,
              child: Text(
                middleBtnText ?? '',
                style: TextStyle(
                    fontSize: 16.sp,
                    color: KqThemeManager.instance
                        .getConfig()
                        .dialogConfig
                        .middleBtnColor ??
                        KqThemeColors.text8C),
              ),
            ),
          ),
        )),
        Visibility(visible: middleBtnText != null, child: KqVerticalDivider(
          height: 48.r,
        ),),
        Expanded(
            child: KqInkWell(
              onTap: () {
                if (onRightBtnTap != null) {
                  onRightBtnTap!(editController.text);
                }
              },
              child: Container(
                alignment: Alignment.center,
                width: double.infinity,
                height: 48.r,
                child: Text(
                  rightBtnText ?? KqIntl.currentResource.confirm,
                  style: TextStyle(
                      fontSize: 16.sp,
                      color: KqThemeManager
                          .getDialogConfig()
                          .mainBtnColor ??
                          KqThemeColors.textLightBlue),
                ),
              ),
            ))
      ],
    ));

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    var padding = MediaQuery
        .of(context)
        .viewInsets
        .bottom;
    return UnconstrainedBox(
        child: Padding(
          padding: EdgeInsets.only(bottom: padding),
          child: SizedBox(
              width: 320.r,
              child: Material(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(
                          KqThemeManager.instance
                              .getConfig()
                              .dialogConfig
                              .radius ??
                              8.r))),
                  color: KqThemeColors.bgWhite,
                  child: LimitedBox(
                    maxHeight: isLangScape ? 350.r : 400.r,
                    child: ClipRRect(
                      borderRadius: BorderRadius.all(Radius.circular(
                          KqThemeManager.instance
                              .getConfig()
                              .dialogConfig
                              .radius ??
                              8.r)),
                      child: dialogChild,
                    ),
                  ))),
        ));
  }
}
